package com.example.myapplication2;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.content.FileProvider;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

public class Mms2Activity extends AppCompatActivity implements View.OnClickListener {
    private ImageView photo11;
    private TextView mmsContent11;
    private TextView titleEdit11;
    private TextView numberEdit11;
    private GridLayout list2;
    private Uri data;
    private String path2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mms2);
        findViewById(R.id.saveMMS2).setOnClickListener(this);
        photo11 = findViewById(R.id.photo11);
        mmsContent11 = findViewById(R.id.mmsContent11);
        titleEdit11 = findViewById(R.id.titleEdit11);
        numberEdit11 = findViewById(R.id.numberEdit11);
        list2 = findViewById(R.id.list2);
        //开启扫描
        MediaScannerConnection.scanFile(this,
                new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString()},
                null, null);
        //加载最新的6个照片
        LoadImageList();
    }

    @SuppressLint("Range")
    private void LoadImageList() {
        ArrayList<HashMap<String,Object>> list=new ArrayList<>();
        String[] columns=new String[]{
                MediaStore.Images.Media._ID, //文件编号
                MediaStore.Images.Media.TITLE, //文件标题
                MediaStore.Images.Media.SIZE, //文件大小
                MediaStore.Images.Media.DATA //文件路径
        };
        Cursor query = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                "_size<3072000", null, "_id DESC");
        if (query!=null){
            int count=0;
            while (query.moveToNext() && count<6){
                long id = query.getLong(query.getColumnIndex(MediaStore.Images.Media._ID));
                String title = query.getString(query.getColumnIndex(MediaStore.Images.Media.TITLE));
                String path = query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));
                long size = query.getLong(query.getColumnIndex(MediaStore.Images.Media.SIZE));
                if (path.contains("Pictures")){
                    count++;
                    HashMap<String,Object> map=new HashMap<>();
                    map.put("id",id);
                    map.put("title",title);
                    map.put("path",path);
                    map.put("size",size);
                    list.add(map);
                }
            }
        }
        list2.removeAllViews();
        int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
        for (HashMap<String, Object> map : list) {
            ImageView imageView = new ImageView(this);
            String path = (String) map.get("path");
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            imageView.setImageBitmap(bitmap);
            imageView.setOnClickListener(view -> {
                photo11.setImageBitmap(bitmap);
                path2=path;

            });

            ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(screenWidth/3,screenWidth/3);
            imageView.setLayoutParams(params);
            System.out.println("...........");
            list2.addView(imageView);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.saveMMS2:
                if (path2!=null){
                    try {
                        sendMMS();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }else {
                    Toast.makeText(this,"请选中图片",Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(this,"正常跳转了",Toast.LENGTH_SHORT).show();
                break;

        }
    }

    private void sendMMS() {
        //兼容Android7.0 把访问文件的Uri改为FileProovider
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            String file_provider = getApplicationContext().getApplicationContext().getPackageName() + ".fileProvider";
            System.out.println("file_provider的值"+file_provider);
            data= FileProvider.getUriForFile(this,file_provider,new File(path2));
        }else {
            data=Uri.parse(path2);
        }
        Intent intent = new Intent(Intent.ACTION_SEND);
        //设置任务栈类型
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //目标号码
        intent.putExtra("address",numberEdit11.getText().toString());
        //标题
        intent.putExtra("subject",titleEdit11.getText().toString());
        //内容
        intent.putExtra("sms_body",mmsContent11.getText().toString());
        //附件
        intent.putExtra(Intent.EXTRA_STREAM,data);
        //附件类型
        intent.setType("image/*");
        //Intent的接受者允许读取intent对象携带的URI数据
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    }
}